function buildBanner(res) {
    const banner_img = document.getElementById("banner-img");
    const banner_indicator = document.getElementById("banner-indicator");
    let innerText1 = '';
    let innerText2 = '';
    for (let i = 0; i < res.data['banners'].length; i++) {
        if (i === 0) {
            innerText1 = innerText1 + '<button type="button" data-bs-target="#banner1" data-bs-slide-to="' + i + '" class="active"></button>';
            innerText2 = innerText2 + '<div class="carousel-item active">\n' +
                '                        <img  src="' + res.data["banners"][i]["imageUrl"] + '" class="d-block" alt="" ">' +
                '                     </div>';
        } else {
            innerText1 = innerText1 + '<button type="button" data-bs-target="#banner1" data-bs-slide-to="' + i + '"></button>';
            innerText2 = innerText2 + '<div class="carousel-item">\n' +
                '                        <img src="' + res.data["banners"][i]["imageUrl"] + '" class="d-block" alt=""">\n' +
                '                    </div>';
        }
    }
    banner_indicator.innerHTML = innerText1;
    banner_img.innerHTML = innerText2;
    innerText1 = null;
    innerText2 = null;
}

function buildHighQuality(res) {
    for (let i = 0; i < res.data['playlists'].length; i++) {
        const playlistx = document.getElementById("highquality-playlist-0" + (i + 1));
        playlistx.innerHTML = '<div onclick="jumpPlayListInfo(' + res.data['playlists'][i]['id'] + ',' + res.data['playlists'][i]['trackCount'] + ')"  class="playlist-image rounded border-end border-5">\n' +
            '                                    <img class="rounded" src="' + res.data['playlists'][i]['coverImgUrl'] + '" alt="">\n' +
            '                                </div>\n' +
            '                                <div onclick="jumpPlayListInfo(' + res.data['playlists'][i]['id'] + ',' + res.data['playlists'][i]['trackCount'] + ')"   class="highquality-name">' + res.data['playlists'][i]['name'] + '</div>\n' +
            '                                <div class="highquality-tags">' + getStringsTags(res.data['playlists'][i]['tags']) + '</div>\n' +
            '                                <div class="highquality-playcount"><i class="bi bi-headphones"></i>' + changePlayCount(res.data['playlists'][i]['playCount']) + '</div>'
    }
}

function buildNewSongs(res) {
    for (let i = 0; i < res.data['albums'].length; i++) {
        const newSongx = document.getElementById("newsong-0" + (i + 1));
        newSongx.innerHTML = '<div onclick="jumpAlbumInfo(' + res.data['albums'][i]['id'] + ',' + res.data['albums'][i]['artist']['id'] + ')" class="newsong-image-msk">\n' +
            '                                    <img class="rounded border-end border-5"\n' +
            '                                         src="' + res.data['albums'][i]['blurPicUrl'] + '"\n' +
            '                                         alt="">\n' +
            '                                    <div class="newsong-msk"></div>\n' +
            '                                </div>\n' +
            '                                <div onclick="jumpAlbumInfo(' + res.data['albums'][i]['id'] + ',' + res.data['albums'][i]['artist']['id'] + ')" class="newsong-name">' + limitStringLength(res.data['albums'][i]['name']) + '</div>\n' +
            '                                <div class="newsong-artists">\n' + getArtistsString(res.data['albums'][i]['artists']) + '</div>';
    }
}

function getBannersData() {
    return axios.get('http://localhost:3000/banner?type=0');
}

function getHighQualityData() {
    return axios.get(`http://localhost:3000/top/playlist/highquality?before=${Date.now()}&limit=4`);
}

function getNewSongsData() {
    return axios.get('http://localhost:3000/album/new?area=All&limit=6');
}

axios.all([getBannersData(), getHighQualityData(), getNewSongsData()])
    .then(axios.spread(function (res1, res2, res3) {
        buildBanner(res1);
        buildHighQuality(res2);
        buildNewSongs(res3);
    })).catch(error => {
    console.log(error);
});

function getStringsTags(tas) {
    for (let i = 0; i < tas.length; i++) {
        let a;
        a = '#' + tas[i];
        tas[i] = a;
    }
    return tas.join(' ');
}

function changePlayCount(count) {
    if (parseInt(count) >= 10000) {
        let a = count / 10000;
        return (parseInt(a + "")) + "w";
    } else {
        return count;
    }
}

function getArtistsString(artists) {
    let names = [];
    for (let i = 0; i < artists.length; i++) {
        if (i >= 2) {
            break;
        }
        let a;
        a = '<span>' + artists[i]['name'] + '</span>'
        names[i] = a;
    }
    if (artists.length <= 2) {
        return names.join('&nbsp;/&nbsp;');
    } else {
        return names.join('&nbsp;/&nbsp;') + '&nbsp;/...';
    }
}

function limitStringLength(str) {
    return str.length > 10 ? str.substring(0, 10) + '..' : str;
}

function useFuncJump(cat) {
    window.parent.controlJumpto(2, '?cat=' + cat);
}

